/**
 * Created by asus on 2017/8/1.
 */
$(function () {
    var isShopOpened=false;
    var isAreaOpened=false;

    $(".shop").click(function () {
        if (isShopOpened){
           closeShopContent()

        }else {
           openShopContent()

        }

    })
    $(".area").click(function () {
        if (isAreaOpened){
            closeAreaContent()
        }else {
           openAreaContent()
        }

    })

    var clickShopedId=0;
    var clickAreaedId=0;

    $(function () {
        $.ajax({
            url:'http://127.0.0.1:3000/api/getgsshop',
            dataType:'json',
            success:function (data) {
                console.log(data)
                var html=template('shop-content-template',data);
                $('.shop-content').html(html);

                $('.shop-content>a').click(function () {
                    $('.shop-content>a').removeClass("haha");
                    $(this).addClass("haha");
                    clickShopedId=this.dataset['shopId'];
                    $('.shop').text($(this).text());


                    closeShopContent();
                    getProductList();
                    return false;
                })

                clickShopedId=data.result[0].shopId;
                var firstShopName=data.result[0].shopName;
                $('.shop').text(firstShopName);

                $(function () {
                    $.ajax({
                        url:'http://127.0.0.1:3000/api/getgsshoparea',
                        dataType:'json',
                        success:function (data) {
                            console.log(data)
                            var html=template('area-content-template',data);
                            $('.area-content').html(html);

                            $('.area-content>a').click(function () {
                                $('.area-content>a').removeClass("haha");
                                $(this).addClass("haha");
                                clickAreaedId=this.dataset['areaId'];
                                $('.area').text($(this).text());

                                closeAreaContent();
                                getProductList();
                                return false;
                            })

                            clickAreaedId=data.result[0].areaId;
                            var firstAreaName=data.result[0].areaName;
                            $('.area').text(firstAreaName);
                            getProductList();
                        }
                    })
                })
            }
        })
    })

  function getProductList() {
      $(function () {
          $.ajax({
              url:'http://127.0.0.1:3000/api/getgsproduct',
              data:{shopid:clickShopedId,areaid:clickAreaedId},
              dataType:'json',
              success:function(data) {
                  console.log(data);
                  var html=template('product-list-template',data);
                  $('.product-list').html(html);

              }
          })
      })
  }


  //高内聚  低耦合
    function openShopContent(){
        closeAreaContent();
        $(".shop-content").css({
            display:'block'
        })
        $('.shop').addClass('opened');
        isShopOpened=true;
    }

    function closeShopContent() {
        $(".shop-content").css({
            display:'none'
        })
        $('.shop').removeClass('opened')
        isShopOpened=false;
    }

    function openAreaContent() {
        closeShopContent();
        $(".area-content").css({
            display:'block'
        })
        $('.area').addClass('opened');
        isAreaOpened=true;
    }
    function closeAreaContent() {
        $(".area-content").css({
            display:'none'
        })
        $('.area').removeClass('opened');
        isAreaOpened=false;
    }
})

